package com.example.batch_demo.job;

import com.example.batch_demo.entity.source.StudentSource;
import com.example.batch_demo.entity.target.StudentTarget;
import jakarta.persistence.EntityManagerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.database.JpaItemWriter;
import org.springframework.batch.item.database.JpaPagingItemReader;
import org.springframework.batch.item.database.builder.JpaItemWriterBuilder;
import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * @Description
 * @Author lihw
 * @Date 2023/12/15 8:40
 */
@Slf4j
@Configuration
public class StudentSyncJobOne {

    @Bean
    public JpaPagingItemReader<StudentSource> studentJpaItemReader(@Qualifier("sourceLocalContainerEntityManagerFactoryBean") EntityManagerFactory entityManagerFactory) {

        return new JpaPagingItemReaderBuilder<StudentSource>()
                .name("studentJpaItemReader")
                .entityManagerFactory(entityManagerFactory)
                .pageSize(2)
                .queryString("select s from StudentSource s")
                .build();
    }

    @Bean
    public JpaItemWriter<StudentTarget> peopleJpaSyncWriter(@Qualifier("targetLocalContainerEntityManagerFactoryBean") EntityManagerFactory entityManagerFactory) {
        // 通过jpa entity manager 的 persist merge 写入
        return new JpaItemWriterBuilder<StudentTarget>()
                .clearPersistenceContext(false) // true: 在开始写入之前，JpaItemWriter将清除持久化上下文中已存在的实体对象
                .usePersist(false)  // true : persist, false : merge
                .entityManagerFactory(entityManagerFactory)
                .build();
    }

    @Bean
    public ItemProcessor<StudentSource, StudentTarget> peopleSyncJpaProcessor() {
        return (item) -> {
            StudentTarget studentTarget = new StudentTarget();
            studentTarget.setStuId(item.getStuId());
            studentTarget.setName(item.getName());
            studentTarget.setAge(item.getAge());
            studentTarget.setClassId(item.getClassId());
            return studentTarget;
        };
    }

    @Bean
    public Step studentSyncStepOne(JobRepository jobRepository,
                                   @Qualifier("targetPlatformTransactionManager") PlatformTransactionManager jpaTransactionManager,
                                   @Qualifier("studentJpaItemReader") JpaPagingItemReader<StudentSource> reader,
                                   @Qualifier("peopleJpaSyncWriter") JpaItemWriter<StudentTarget> writer,
                                   @Qualifier("peopleSyncJpaProcessor") ItemProcessor<StudentSource, StudentTarget> processor) {

        return new StepBuilder("studentSyncStepOne", jobRepository)
                .<StudentSource, StudentTarget>chunk(2, jpaTransactionManager)
                .reader(reader)
                .processor(processor)
                .writer(writer)
                .build();
    }

    @Bean
    public Job studentJpaSyncJob(@Qualifier("studentSyncStepOne") Step studentSyncStepOne,
                                 JobRepository jobRepository) {
        log.info("注册job: StudentSyncJobOne ");
        return new JobBuilder("StudentSyncJobOne", jobRepository)
                .start(studentSyncStepOne)
                .build();

    }

}
